package comment

import (
	"enjoy-shop/api/base"
	"enjoy-shop/formdata"
	"enjoy-shop/server/comment"
	"enjoy-shop/server/orderclient"
	"errors"
	"github.com/gin-gonic/gin"
)

func GetCommentList(c *gin.Context) {
	var data formdata.GetCommentList
	err := c.ShouldBindJSON(&data)
	if err != nil {
		base.RespData(c, "", err)
		return
	}

	list, err := comment.GetComment(&data)
	base.RespData(c, list, err)
}

func GetCommentNumByRat(c *gin.Context) {
	var data formdata.GetCNumByRat
	err := c.ShouldBindJSON(&data)
	if err != nil {
		base.RespData(c, "", err)
		return
	}
	num, err := comment.GetCommentNumByRating(&data)
	base.RespData(c, num, err)
}
func GetOrderById(c *gin.Context) {
	var data formdata.GetOrder
	err := c.ShouldBindJSON(&data)
	if err != nil {
		base.RespData(c, "", err)
		return
	}
	order, err := orderclient.GetOrder(data.OrderId)
	if err != nil {
		base.RespData(c, "", err)
		return
	}
	switch order.Status {
	case 5:
		base.RespData(c, "", errors.New("已评论"))
		return
	case 3:
		err = orderclient.UpdateOrder(data.OrderId, 5)
		if err != nil {
			base.RespData(c, "", err)
			return
		}
	default:
		base.RespData(c, "", errors.New("数据错误"))
		return
	}

	base.RespData(c, order, nil)
}
